﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoShopping.Model;

namespace GoShopping.BLL
{
    /// <summary>
    /// 供货厂商维护类
    /// </summary>
    public class ProductSuplyBean
    {

        /// <summary>
        /// 查询一个供货厂商
        /// </summary>
        /// <param name="prdctSuplyID"></param>
        /// <returns></returns>
        public PrdctSupplyer GetProductSuply(int prdctSuplyID)
        {
            return Databases.DBGoShopping.Select(GoShoppingNBear.PrdctSupplyer).Where(
                GoShoppingNBear.PrdctSupplyer.ID == prdctSuplyID).ToSingleObject<PrdctSupplyer>();
        }

        /// <summary>
        /// 获取所有供应商列表
        /// </summary>
        /// <returns></returns>
        public IList<PrdctSupplyer> GetAllProductSuplyList()
        {
            return Databases.DBGoShopping.Select(GoShoppingNBear.PrdctSupplyer).ToList<PrdctSupplyer>();
        }

        /// <summary>
        /// 删除供应商
        /// </summary>
        /// <param name="prdctSuplyID"></param>
        /// <returns></returns>
        public bool DelPrdctSuply(string prdctSuplyID)
        {
            int i = Databases.DBGoShopping.Delete(GoShoppingNBear.PrdctSupplyer).Where(
                GoShoppingNBear.PrdctSupplyer.ID == prdctSuplyID).Execute();
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一个供应商
        /// </summary>
        /// <param name="suplyer"></param>
        /// <returns></returns>
        public bool UpdatePrdctSuply(PrdctSupplyer suplyer)
        {
            int i = Databases.DBGoShopping.Update(GoShoppingNBear.PrdctSupplyer).AddColumn(
                GoShoppingNBear.PrdctSupplyer.Address ,suplyer.Address).AddColumn(
                GoShoppingNBear.PrdctSupplyer.CompanyFullName,suplyer.CompanyFullName).AddColumn(
                GoShoppingNBear.PrdctSupplyer.Name,suplyer.Name).AddColumn(
                GoShoppingNBear.PrdctSupplyer.Remark,suplyer.Remark).AddColumn(
                GoShoppingNBear.PrdctSupplyer.Telephone,suplyer.Telephone).Where(
                GoShoppingNBear.PrdctSupplyer.ID == suplyer.ID).Execute();
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 添加一个供应商
        /// [ID字段都不用管，都是自己生成]
        /// </summary>
        /// <param name="suplyer"></param>
        /// <returns></returns>
        public bool AddPrdctSuply(PrdctSupplyer suplyer)
        {
            int i = Databases.DBGoShopping.Select(GoShoppingNBear.PrdctSupplyer, GoShoppingNBear.PrdctSupplyer.ID.Count()).Where(
                GoShoppingNBear.PrdctSupplyer.Name == suplyer.Name).ToScalar<int>();
            if (i > 0)
            {
                return false;
            }

            i = Databases.DBGoShopping.Insert(GoShoppingNBear.PrdctSupplyer).AddColumn(
                GoShoppingNBear.PrdctSupplyer.Address, suplyer.Address).AddColumn(
                GoShoppingNBear.PrdctSupplyer.CompanyFullName, suplyer.CompanyFullName).AddColumn(
                GoShoppingNBear.PrdctSupplyer.Name, suplyer.Name).AddColumn(
                GoShoppingNBear.PrdctSupplyer.Remark, suplyer.Remark).AddColumn(
                GoShoppingNBear.PrdctSupplyer.Telephone, suplyer.Telephone).Execute();
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
